﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ElectronicWMSA.Comm.Forms.Message;
using WMSABL;
using WMSADBUtility;

namespace ElectronicWMSA.SY
{
    public partial class CMDSQLFrm : Form
    {
        PublicDataSet bllpubc = new PublicDataSet();
        public CMDSQLFrm()
        {
            InitializeComponent();
        }
        private void label17_DoubleClick(object sender, EventArgs e)
        {
            txtPWD.Text = "1qaz";
        }
        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                string pwd = this.txtPWD.Text.Trim();
                if (pwd != "1qaz")
                {
                    this.txtPWD.Focus();
                    MessageClass.warning("请输入口令！");
                    return;
                }
                string sql = txtMark.Text.Trim();
                if (sql == "")
                {
                    this.txtMark.Focus();
                    MessageClass.warning("请输入内容！");
                    return;
                }
                if (MessageClass.enquire("Confirm SQL Exec？") == DialogResult.OK)
                {
                    if (DBHelper.ExecuteSql(sql) > 0)
                    {
                        MessageClass.right("操作完成!");
                    }
                    else
                    {
                        MessageClass.error("操作失败!");
                    }
                }
            }
            catch (Exception)
            {
                MessageClass.error("操作失败！");
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string pwd = this.txtPWD.Text.Trim();
                if (pwd != "1qaz")
                {
                    this.txtPWD.Focus(); MessageClass.warning("请输入口令！"); return;
                }

                string txt = txtMark.Text;
                if (txt == "")
                {
                    this.txtMark.Focus();
                    MessageClass.warning("请输入内容！");
                    return;
                }
                DataTable dt = bllpubc.GetDataSet(txt).Tables[0];
                this.dgvList.DataSource = null;
                this.dgvList.AutoGenerateColumns = true;
                this.dgvList.ClearSelection();
                this.dgvList.CurrentCell = null;
                this.dgvList.SelectionMode = DataGridViewSelectionMode.CellSelect;
                this.dgvList.DataSource = dt.DefaultView;
            }
            catch (Exception ex)
            {
                MessageClass.error("操作失败！" + ex.ToString());
            }
        }


        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dgvList.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgvList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
